package com.farsight.leo.fssmartlock.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.db.AuthCodeEntity;
import com.farsight.leo.fssmartlock.utils.DateUtils;

import java.util.ArrayList;
import java.util.Date;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * Author: Administrator
 * Date: 2019/9/17
 * Project: SmartLockApp
 * Description:
 */
public class AuthCodeListAdapter extends BaseAdapter {
    private ArrayList<AuthCodeEntity> mList;
    private Context mContext;
    private Date mDate;


    public AuthCodeListAdapter(ArrayList<AuthCodeEntity> list, Context context) {
        mList = list;
        mContext = context;
        mDate = new Date();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if(convertView==null){
            convertView=LayoutInflater.from(mContext).inflate(R.layout.item_auth_code_list,null );
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else {
            vh= (ViewHolder) convertView.getTag();
        }

        AuthCodeEntity entity = mList.get(position);
        int color;
        if (entity.getValidDate() > mDate.getTime()) {
            color = mContext.getResources().getColor(R.color.colorBlack);
        } else {
            color = mContext.getResources().getColor(R.color.colorRed);
        }
        vh.mTvDate.setTextColor(color);
        vh.mTvDate.setText(DateUtils.parseLongToOutputTimeFormat(entity.getValidDate()));
        vh.mTvCode.setText(entity.getAuthCode());
        return convertView;
    }

    private class ViewHolder {
        AppCompatTextView mTvCode;
        AppCompatTextView mTvDate;

        private ViewHolder(View contentView) {
            mTvCode = contentView.findViewById(R.id.item_auth_code_list_tv_auth_code);
            mTvDate = contentView.findViewById(R.id.item_auth_code_list_tv_valid_date);
        }
    }
}
